#include "stdafx.h"
#include ".\network.h"
#include <IOCPServer.h>
#include <Macro.h>
/*
Network::Network()
	:	m_pIOCPServer( NULL )
{
}

Network::~Network()
{
	if( m_pIOCPServer )
	{
		delete m_pIOCPServer;
		m_pIOCPServer = NULL;
	}
}

VOID Network::Init(	fnOnAcceptServer _fnOnServerAccept,	fnOnDisconnectServer _fnOnServerDisconnect, 
					fnOnConnectServer _fnOnServerConnect, fnOnRecvServer _fnOnServerRecv,
					fnOnAcceptClient _fnOnClientAccept,	fnOnDisconnectClient _fnOnClientDisconnect, 
					fnOnConnectClient _fnOnClientConnect, fnOnRecvClient _fnOnClientRecv )
{

	IOHANDLER_DESC desc[2];

	desc[0].dwIoHandlerKey				= CLIENT_IOHANDLER;
	desc[0].dwMaxConnection				= 7000;
	desc[0].dwSendBufferSize			= 60000;
	desc[0].dwRecvBufferSize			= 60000;
	desc[0].dwTimeOut					= 30000;
	desc[0].dwNumberOfIoThreads			= 1;
	desc[0].dwNumberOfConnectThreads	= 1;
	desc[0].dwMaxPacketSize				= 4096;
	desc[0].fnOnAccept					= _fnOnClientAccept;
	desc[0].fnOnDisconnect				= _fnOnClientDisconnect;
	desc[0].fnOnConnect					= _fnOnClientConnect;
	desc[0].fnOnRecv					= _fnOnClientRecv;

	desc[1].dwIoHandlerKey				= SERVER_IOHANDLER;
	desc[1].dwMaxConnection				= 10;
	desc[1].dwSendBufferSize			= 60000;
	desc[1].dwRecvBufferSize			= 60000;
	desc[1].dwTimeOut					= 30000;
	desc[1].dwNumberOfIoThreads			= 1;
	desc[1].dwNumberOfConnectThreads	= 1;
	desc[1].dwMaxPacketSize				= 4096;
	desc[1].fnOnAccept					= _fnOnServerAccept;
	desc[1].fnOnDisconnect				= _fnOnServerDisconnect;
	desc[1].fnOnConnect					= _fnOnServerConnect;
	desc[1].fnOnRecv					= _fnOnServerRecv;
	
	ASSERT( !m_pIOCPServer );
	m_pIOCPServer = new IOCPServer;
	m_pIOCPServer->Init( desc, 2 );
	
}

VOID Network::Release()
{
	m_pIOCPServer->ShutDown();
}

BOOL Network::StartServer( char * szIP, WORD wPort )
{
	if( !m_pIOCPServer->StartListen( SERVER_IOHANDLER, szIP, wPort ) )
	{
		return FALSE;
	}

	return TRUE;
}

BOOL Network::StartClient( char * szIP, WORD wPort )
{
	if( !m_pIOCPServer->StartListen( CLIENT_IOHANDLER, szIP, wPort ) )
	{
		return FALSE;
	}

	return TRUE;
}

VOID Network::ConnectToClient( char * szIP, WORD wPort )
{
	m_pIOCPServer->Connect( CLIENT_IOHANDLER, szIP, wPort );
}

VOID Network::ConnectToServer( char * szIP, WORD wPort )
{
	m_pIOCPServer->Connect( SERVER_IOHANDLER, szIP, wPort );
}

VOID Network::SendToServer( DWORD dwIndex, BYTE * pMsg, WORD wSize )
{
	m_pIOCPServer->Send( SERVER_IOHANDLER, dwIndex, pMsg, wSize );
}

VOID Network::SendToClient( DWORD dwIndex, BYTE * pMsg, WORD wSize )
{
	m_pIOCPServer->Send( CLIENT_IOHANDLER, dwIndex, pMsg, wSize );
}

VOID Network::DisconnectServer( DWORD dwIndex )
{
	m_pIOCPServer->Disconnect(SERVER_IOHANDLER, dwIndex );
}

VOID Network::DisconnectClient( DWORD dwIndex )
{
	m_pIOCPServer->Disconnect(CLIENT_IOHANDLER, dwIndex );
}
VOID Network::Update()
{
	m_pIOCPServer->Update();
}

*/